package Phase1;

import java.net.InetAddress;
import java.net.UnknownHostException;

/** 
 * @domain 
 * valid InetAddress, presenting legal IP address and its name.
 *This class contains the ip address and hostname of a target machine.
 * @author byu.ryanpadilla
 * @invariant 
 *
 */

public class MachineID implements java.io.Serializable
{
	 
	private static final long serialVersionUID = 1L;

	
	private InetAddress address;
	
	/**
	 * Creates an ip address and host name that points to the local machine.
	 * @throws UnknownHostException
	 */
	
	public MachineID()
	{
		try 
		{
			address = InetAddress.getLocalHost();
		} 
		catch (UnknownHostException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Creates an ip address and host name from the given parameter that uniquely identifies
	 * a machine on the network
	 * @param id Ip address or host name that uniquely identifies a machine.
	 * @throws UnknownHostException
	 */
	public MachineID(String id)throws UnknownHostException
	{
		this.address = InetAddress.getByName(id);
	}
	/**
	 * IP address getter.
	 * @return Stored IP address
	 */
	public String getIP()
	{
		return address.getHostAddress();
	}
	/**
	 * Host name getter.
	 * @return Stored Host Name.
	 */
	public String getHost()
	{
		return address.getHostName();
	}
	

	/**
	 * InetAddress getter.
	 * @return Stored InetAddress.
	 */
	public InetAddress getInetAddress()
	{
		return address;
	}
	
	/**
	 * Creates String representation of the IP and Jost name.
	 * @return String of IP and Host name. 
	 */
	
	public String toString()
	{
		return "Host: " + address.getHostName() + "\nIP: " + address.getHostAddress() + "\n";
	}
	
	/**
	 * Creates a unique Hash code for the stored IP address and Host name.
	 * 
	 *@return Unique Hash code for the stored IP address and Host name.
	 */
	public int hashCode()
	{
		return address.getHostAddress().hashCode() + address.getHostName().hashCode();
	}
	
}
